package org.fyp.synchrona.util;

import java.util.LinkedList;
import java.util.Queue;

import org.fyp.synchrona.communication.RequestHandler;

	/**
	 * This class is used to queue data sent from Openfire server. 
	 * @author root
	 *
	 */

public class DataQueue {
	
	private static DataQueue qu = null;
	
	private static Queue q ;
	
	private DataQueue(){
				
	}
	
	public static synchronized DataQueue getDataQueueObject(){
		
		if(qu == null){
			
			qu = new DataQueue();
			q = new LinkedList();
			return qu;
			
		}else{
			
			return qu;
			
		}
		
	}
	
	public static void addElement(PacketIn element){	
		
		qu.getQueue().add(element);
		
	}
	
	public static Queue<PacketIn> getQueue(){
		
		return q;
		
	}
	
	public static int getQueueSize(){
		
		return DataQueue.getQueue().size();
		
	}
	
	public  static void enqueue(PacketIn obj) {
		//logger.info("Object added");
		//qu.getQueue().add(obj);
		//RequestHandler.getRequestHandler().notify();
		synchronized (DataQueue.getDataQueueObject()) {
			DataQueue.getQueue().add(obj);
			//DataQueue.getDataQueueObject().notifyAll();
		}

		
	}
	
	public static PacketIn dequeue() throws InterruptedException {
		
		synchronized (DataQueue.getDataQueueObject()) {
		    while (DataQueue.getQueue().isEmpty()) {
		    	DataQueue.getDataQueueObject().wait(5000);
		    }
		}
		//while (qu.getQueue().isEmpty()) {
		//	System.out.println("[INFO] :i am waiting send me data");
		//	wait();
		//}
		return (PacketIn) qu.getQueue().remove();
	}

//	public static boolean isDataExist(){
//		if(DataQueue.getQueue().size()>0){
//			
//		}else{
//			
//		}
//	}
}
